<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        :placeholder="$t('product.name')"
        v-model="listQuery.filters.name"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"/>
      <el-select
        v-model="listQuery.filters.productCategory"
        :placeholder="$t('product.productCategory')"
        clearable
        style="width: 120px"
        class="filter-item">
        <el-option
          v-for="item in productCategoryList"
          :key="item.key"
          :label="item.label"
          :value="item.key"/>
      </el-select>
      <el-select
        v-model="listQuery.filters.productType"
        :placeholder="$t('product.productType')"
        clearable
        style="width: 120px"
        class="filter-item">
        <el-option
          v-for="item in productTypeList"
          :key="item.key"
          :label="item.label"
          :value="item.key"/>
      </el-select>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter">{{ $t('product.search') }}</el-button>
      <el-select
        v-model="productCategory"
        style="width: 140px"
        class="filter-item">
        <el-option
          v-for="item in productCategoryList"
          :key="item.key"
          :label="item.label"
          :value="item.key"/>
      </el-select>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate">{{ $t('product.add') }}</el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :key="tableKey"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange">
      <el-table-column
        :label="$t('product.id')"
        prop="id"
        align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('product.name')"
        align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('product.productCategory')"
        align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.productCategory }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('product.productType')"
        align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.productType }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('product.createtime')"
        align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.createtime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('product.actions')"
        align="center"
        class-name="small-padding fixed-width"
        width="200">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="handleUpdate(scope.row)">{{ $t('product.edit') }}</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleModifyStatus(scope.row,'deleted')">{{ $t('product.delete') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.pageSize"
      @pagination="getList" />

    <smgqform
      ref="smgqform"
      @submit="handleFilter"/>
    <jjform
      ref="jjform"
      @submit="handleFilter"/>

  </div>
</template>

<script>
import { fetchList, remove, add, edit } from '@/api/product'
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

import smgqform from './smgqform'
import jjform from './jjform'

export default {
  name: 'IndexProduct',
  components: { Pagination, smgqform,jjform },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        pageSize: 20,
        order: 'desc',
        sort: 'id',
        filters: {
          name: '',
          productCategory: '',
          productType: ''
        }
      },
      productCategory: '',
      productCategoryList: [
        {
          key: '基金',
          label: '基金'
        },
        {
          key: '信托',
          label: '信托'
        },
        {
          key: '资管',
          label: '资管'
        },
        {
          key: '私募股权',
          label: '私募股权'
        }
      ],
      productTypeList: [
        {
          key: '天赋集团',
          label: '天赋集团'
        },
        {
          key: '其它',
          label: '其它'
        }
      ],
      sortOptions: [{ label: 'ID Ascending', key: '+id' }, { label: 'ID Descending', key: '-id' }],
      textMap: {
        update: 'Edit',
        create: 'Create'
      },
      pvData: [],
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 0.5 * 1000)
      })
    },
    handleFilter() {
      this.getList()
    },
    handleModifyStatus(row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        remove(row.id).then((res) => {
          if (res.data.code == 1) {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.handleFilter()
          } else {
            this.$message({
              showClose: true,
              message: '该产品下已经有订单存在不能删除',
              type: 'error'
            })
            return false;
          }
        })
      }).catch(() => {

      })
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'id') {
        this.sortByID(order)
      }
    },
    sortByID(order) {
      if (order === 'ascending') {
        this.listQuery.sort = '+id'
      } else {
        this.listQuery.sort = '-id'
      }
      this.handleFilter()
    },
    handleCreate() {
      switch (this.productCategory) {
        case '私募股权':
          this.$refs.smgqform.formShow({}, '私募股权')
          break
        case '基金':
          this.$refs.jjform.formShow({}, '基金')
          break
        case '信托':
          this.$refs.jjform.formShow({}, '信托')
          break
        case '资管':
          this.$refs.jjform.formShow({}, '资管')
          break
        default :
          this.$message({
            showClose: true,
            message: '请产品选择分类',
            type: 'warning'
          })
          break
      }
    },
    handleUpdate(row) {
      var row = Object.assign({}, row) // copy obj
      switch (row.productCategory) {
        case '私募股权':
          this.$refs.smgqform.formShow(row.id, '私募股权', 1)
          break
        case '基金':
          this.$refs.jjform.formShow(row.id, '基金', 1)
          break
        case '信托':
          this.$refs.jjform.formShow(row.id, '信托', 1)
          break
        case '资管':
          this.$refs.jjform.formShow(row.id, '资管', 1)
          break
        default :
          this.$message({
            showClose: true,
            message: '请产品选择分类',
            type: 'warning'
          })
          break
      }
    }
  }
}
</script>
